<?php 
session_start();

include 'connexion.php';
    
    $sql_bien = "SELECT * FROM bien ORDER BY id desc";
    $req_bien = mysqli_query($db,$sql_bien) or die('Erreur SQL !'.$sql_bien.'<br>'.mysqli_error($db)); 


    while ($data_bien = mysqli_fetch_array($req_bien)) {

        $date = date("Y-m-d");

        $sql_date_fin = "SELECT date_fin from reservation where id_bien =".$data_bien['id'];
        $req_date_fin = mysqli_query($db,$sql_date_fin) or die('Erreur SQL !'.$sql_date_fin.'<br>'.mysqli_error($db)); 
        $date_fin_bien = mysqli_fetch_array($req_date_fin);

        if ($date>=$date_fin_bien['date_fin']) {

            $sql_bien_fin = "UPDATE bien SET fin=1 where id=".$data_bien['id'];
            $req_bien_fin = mysqli_query($db,$sql_bien_fin) or die('Erreur SQL !'.$sql_bien_fin.'<br>'.mysqli_error($db));

        }
        
    }
    
    
    $sql = "SELECT * FROM bien ORDER BY id desc";
    $req = mysqli_query($db,$sql) or die('Erreur SQL !'.$sql.'<br>'.mysqli_error($db)); 
    

?> 

<html>
    <head>
          <link rel="stylesheet" type="text/css" href="style.css"/>
      <meta charset="UTF-8" />
      <meta name="description" content="Free Bed ou comment trouver un endroit pour dormir en 2 cliques."/>
      <title>Free Bed</title>
    </head>
    <body>
      <div id="conteneur">
            <div id="header">
                <?php include_once("header.php"); ?>
        </div><br/>
            <div id="contenu">
                <?php 
                        while ( $data= mysqli_fetch_array($req))
                          {
                                $sql_fin = "SELECT fin FROM bien where id=".$data['id'];
                                $res_fin = mysqli_query($db,$sql_fin) or die('Erreur SQL !'.$sql_fin.'<br>'.mysqli_error($db)); 
                                $fin = mysqli_fetch_array($res_fin);

                            if ($fin['0'] != 1)
                            {
                                $sql_type = "SELECT type FROM type where id=".$data['id_type'];
                                $res_type = mysqli_query($db,$sql_type) or die('Erreur SQL !'.$sql_type.'<br>'.mysqli_error($db)); 
                                $data1 = mysqli_fetch_array($res_type);

                                $sql_tarif = "SELECT * FROM tarif where id=".$data['id_tarif'];
                                $res_tarif = mysqli_query($db,$sql_tarif) or die('Erreur SQL !'.$sql_tarif.'<br>'.mysqli_error($db));
                                $data2 = mysqli_fetch_array($res_tarif);

                                $sql_pays = "SELECT pays FROM pays where id=".$data['id_pays'];
                                $res_pays = mysqli_query($db,$sql_pays) or die('Erreur SQL !'.$sql_pays.'<br>'.mysqli_error($db)); 
                                $data3 = mysqli_fetch_array($res_pays);

                                $sql_user = "SELECT nom FROM user where id=".$data['id_user'];
                                $res_user = mysqli_query($db,$sql_user) or die('Erreur SQL !'.$sql_user.'<br>'.mysqli_error($db));
                                $data4 = mysqli_fetch_array($res_user);

                                $sql_photo = "SELECT photo FROM photo where id_bien=".$data['id'];
                                $res_photo = mysqli_query($db,$sql_photo) or die('Erreur SQL !'.$sql_photo.'<br>'.mysqli_error($db));
                                $data5 = mysqli_fetch_array($res_photo);

                                if ($data5['photo'] == "") {
                                    $sql_photo = "SELECT photo FROM photo where id_bien=0";
                                    $res_photo = mysqli_query($db,$sql_photo) or die('Erreur SQL !'.$sql_photo.'<br>'.mysqli_error($db));
                                    $data5 = mysqli_fetch_array($res_photo);
                                }

                                $sql_date = "SELECT * FROM reservation where id_bien=".$data['id'];
                                $res_date = mysqli_query($db,$sql_date) or die('Erreur SQL !'.$sql_date.'<br>'.mysqli_error($db));
                                $data6 = mysqli_fetch_array($res_date);

                                $sql_ville = "SELECT ville FROM ville where id=".$data['id_ville'];
                                $res_ville = mysqli_query($db,$sql_ville) or die('Erreur SQL !'.$sql_ville.'<br>'.mysqli_error($db)); 
                                $data7 = mysqli_fetch_array($res_ville);


                                echo " <center> <table>
                                            <tr>
                                                <td rowspan=4>
                                                    <img src=".$data5['photo'].">
                                                </td>
                                                <td>
                                                    Description : ".utf8_encode(stripslashes($data['description']))."
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Lieu : ".$data7['ville'].", ".$data3['pays']."
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Type : ".$data1['type']."
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Louable à partir de : ".$data6['date_debut']." à ".$data6['date_fin']."
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Mise en ligne par : ".$data4['nom']."
                                                </td>
                                                <td>
                                                    Pour voir l'annonce cliquer <a href='annonce.php?id=".$data['id']."'>ICI</a>
                                                </td>
                                            </tr>
                                        </table> </center> </br><hr/>";
                                    }
                          }
                ?>
            </div>
            <div id="footer">
                <?php include_once("footer.html"); ?>

            </div>

      </div>
    </body>
</html>